package com.showgood.model {
	import com.showgood.core.AbstractProxy;
	import com.showgood.config.SangoConst;
	import com.showgood.util.NetResLoader;
	import com.showgood.util.AssetsUtil;
	import com.showgood.util.NetResLoaderEvent;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObjectContainer;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.events.SecurityErrorEvent;
	import flash.utils.Dictionary;
	import flash.utils.getQualifiedClassName;
	import org.as3commons.collections.LinkedMap;
	import org.puremvc.as3.patterns.proxy.Proxy;
	
	/**
	 * 预加载资源
	 * 
	 * @author bianpeng
	 */
	public class PreloadProxy extends AbstractProxy {
		// 类型的一个dict
		private var _clazzDict:Dictionary = new Dictionary();
		// bitmapdata的一个dict
		private var _bitmapdataDict:LinkedMap = new LinkedMap();

		private var _loadingList:Vector.<NetResLoader>;
		private var _loadingIndex:int;
		
		public function PreloadProxy(data:Object = null) {
			super(data);
			
			_loadingList = new Vector.<NetResLoader>();
			_loadingIndex = 0;
			
			AssetsUtil.instance.prepareLoaders(_loadingList);
			load();
		}
		
		private function load():void {
			var loader:NetResLoader;
			if (_loadingIndex < _loadingList.length) {
				loader = _loadingList[_loadingIndex];
				
				//loader.addEventListener(ResLoaderEvent.RES_LOADED, completeHandler);
				//loader.addEventListener(IOErrorEvent.IO_ERROR, errorHandler);
				//loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, errorHandler);
				//loader.addEventListener(ProgressEvent.PROGRESS, progressHandler);
				loader.addEventListener(NetResLoaderEvent.COMPLETE, completeHandler);
				
				loader.load();
			}
			else {
				initBitmapDataDict();
				_logger.info('preload resource finished!');
				// 到这里载入完毕, 可以初始化socket等了
				// socket连接上之后, 会发login命令
				// 原来有个bug:
				// 不等socket连接上, 就先发login命令了, 造成无尽的等待
				// sangoFacade.registerProxyByType(new SocketProxy());
				sangoFacade.sendNotification(SangoConst.PRELOAD_RESOURCE_DONE);
				//sangoFacade.sendNotification(SangoConst.SWITCH_LOGIN_VIEW);
			}
		}
		private function initBitmpData(name:String , w:int, h:int):void {
			var _cls:Class = getClass(name);
			var _bmp:BitmapData = new _cls(w, h);
			_bitmapdataDict.add(name, _bmp);
		}
		private function initBitmapDataDict():void {
			initBitmpData('city_001', 74, 65);
			initBitmpData('city_002', 74, 65);
		}
		private function completeHandler(event:NetResLoaderEvent):void {
			_loadingIndex++;
			if (event._data) {
				for (var i:int = 0; i < event._data.length; ++i) {
					_clazzDict[getQualifiedClassName(event._data[i])] = event._data[i];
				}
			}
			else {
				_clazzDict[event._name] = event._content;
			}
			load();
		}
		
		/**
		 * 获得加载过来的类
		 * @param	name
		 * @return
		 */
		public function getClass(name:String):Class {
			return _clazzDict[name] as Class;
		}
		private function errorHandler(event:Event):void {
			_logger.error('preload error');
		}
		public function getBitmapData(name:String):BitmapData {
			return _bitmapdataDict.itemFor(name);
		}
	}

}


